package org.example.ui.customer;

import org.example.business.MilkTeaBusiness;
import org.example.business.AddToCartBusiness;
import org.example.model.MilkTea;
import org.example.model.Order;
import org.example.model.OrderItem;
import org.example.ui.milk_order.MilkTeaItemPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DispositionPanel extends JPanel {
    private final JLabel temp, sugar,
            ingredient, milkLabel,
            priceLabel, dispositionLabel,
            numberLabel;
    private JButton[] tempButton, sugarButton, ingredientButton;
    private final JButton buyNow, addToCart, add, subtract;
    private MilkTea milkTea = new MilkTea();
    private String t, s, in;
    private Order order;
    private OrderItem orderItem;
    public DispositionPanel(DispositionSettleDialog dialog) {
        setBounds(0,0,500,700);
        setLayout(null);
        buyNow = new JButton("立即购买");
        numberLabel = new JLabel("1");
        addToCart = new JButton("加入购物车");
        add = new JButton("+");
        subtract = new JButton("-");
        add.setBounds(400,470,50,50);
        subtract.setBounds(300,470,50,50);
        numberLabel.setBounds(360,470,40,40);
        add.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int num = Integer.parseInt(numberLabel.getText());
                num ++;
                if (num <= milkTea.inventory) {
                    numberLabel.setText("" + num);
                    orderItem.number = num;
                }
                else {
                    JOptionPane.showMessageDialog(DispositionPanel.this, "奶茶库存不足");
                }
            }
        });
        subtract.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int num = Integer.parseInt(numberLabel.getText());;
                num --;
                if (num != 0) {
                    numberLabel.setText("" + num);
                    orderItem.number = num;
                }
            }
        });
        add(numberLabel);
        add(add);
        add(subtract);
        add(buyNow);
        add(addToCart);
        buyNow.setBounds(100,570,100,50);
        addToCart.setBounds(300,570,100,50);
        buyNow.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                order.orderItems.add(orderItem);
                order.setDate();
                order.setTotal();
                order.setTotalPrice();
                dialog.dispose();
                new DispositionSettleDialog(order, 3).setVisible(true);
            }
        });
        addToCart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int n = 0;
                int num = Integer.parseInt(numberLabel.getText());
                AddToCartBusiness.itemMap.get(orderItem.milkName).setNumberLabel(num);
                for (OrderItem item : AddToCartBusiness.cart.orderItemList) {
                    if (item.milkName.equals(milkTea.milkName) &&
                            item.imagePath.equals(milkTea.imagePath) &&
                            item.specification.equals(dispositionLabel.getText())) {
                        item.number += num;
                        n++;
                        break;
                    }
                }
                if (AddToCartBusiness.map.containsKey(milkTea.milkName)) {
                    AddToCartBusiness.map.put(milkTea.milkName, AddToCartBusiness.map.get(milkTea.milkName) + num);
                } else {
                    AddToCartBusiness.map.put(milkTea.milkName, num);
                }
                if (n == 0) {
                    OrderItem item = new OrderItem(milkTea, dispositionLabel.getText(), num);
                    AddToCartBusiness.cart.addOrderItem(item);
                }
                AddToCartBusiness.setOrderItems();
                dialog.dispose();
            }
        });
        milkLabel = new JLabel();
        priceLabel = new JLabel("9.9");
        priceLabel.setFont(new Font("",Font.BOLD,25));
        dispositionLabel = new JLabel();
        dispositionLabel.setFont(new Font("",Font.BOLD,15));
        priceLabel.setBounds(20,460,50,30);
        dispositionLabel.setBounds(20,480,600,40);
        milkLabel.setBounds(0,0,500,100);
        milkLabel.setFont(new Font("",Font.BOLD,30));
        temp = new JLabel("温度:");
        sugar = new JLabel("糖度:");
        ingredient = new JLabel("加料:");
        tempButton = new JButton[3];
        sugarButton = new JButton[5];
        ingredientButton = new JButton[4];
        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.add(milkLabel);
        jPanel.add(temp);
        jPanel.add(sugar);
        jPanel.add(ingredient);
        add(priceLabel);
        add(dispositionLabel);
        JScrollPane scrollPane = new JScrollPane(jPanel);
        scrollPane.setBounds(0,0,500,460);
        scrollPane.getVerticalScrollBar().setUnitIncrement(20);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jPanel.setPreferredSize(new Dimension(500,600));
        add(scrollPane);

        String[] temps = {"少冰","正常冰","常温"},
                sugars = {"正常糖","七分糖","五分糖","三分糖","不额外加糖"},
                ingredients = {"珍珠","脆啵啵","椰果","手圆"};
        for(int i = 0; i<3; i++) {
            tempButton[i] = new JButton(temps[i]);
            tempButton[i].setBackground(Color.WHITE);
            jPanel.add(tempButton[i]);
            tempButton[i].setBounds(20+(i+1)*110,170,100,50);
        }
        int j = 0, k = 0;
        for (int i = 0; i<5; i++){
            sugarButton[i] = new JButton(sugars[i]);
            sugarButton[i].setBackground(Color.WHITE);
            jPanel.add(sugarButton[i]);
            k += 1;
            if(i%3==0&&i!=0) {
                k = 1;
                j += 1;
            }
            sugarButton[i].setBounds(20+k*110,270+j*50,100,50);
        }
        int j1 = 0, k1 = 0;
        for(int i = 0; i<4; i++) {
            ingredientButton[i] = new JButton(ingredients[i]);
            ingredientButton[i].setBackground(Color.WHITE);
            jPanel.add(ingredientButton[i]);
            k1 += 1;
            if(i%3==0&&i!=0) {
                k1 = 1;
                j1 += 1;
            }
            ingredientButton[i].setBounds(20+k1*110,370+j*50+j1*50,100,50);
        }
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            sugarButton[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    sugarButton[finalI].setBackground(Color.CYAN);
                    s = sugarButton[finalI].getText();
                    dispositionLabel.setText(t + "/" + s + "/" + in);
                    if (finalI == 0) {
                        sugarButton[2].setBackground(Color.WHITE);
                        sugarButton[1].setBackground(Color.WHITE);
                        sugarButton[3].setBackground(Color.WHITE);
                        sugarButton[4].setBackground(Color.WHITE);
                    }
                    if (finalI == 1) {
                        sugarButton[0].setBackground(Color.WHITE);
                        sugarButton[2].setBackground(Color.WHITE);
                        sugarButton[3].setBackground(Color.WHITE);
                        sugarButton[4].setBackground(Color.WHITE);
                    }
                    if (finalI == 2) {
                        sugarButton[0].setBackground(Color.WHITE);
                        sugarButton[3].setBackground(Color.WHITE);
                        sugarButton[4].setBackground(Color.WHITE);
                        sugarButton[1].setBackground(Color.WHITE);
                    }
                    if (finalI == 3) {
                        sugarButton[0].setBackground(Color.WHITE);
                        sugarButton[2].setBackground(Color.WHITE);
                        sugarButton[4].setBackground(Color.WHITE);
                        sugarButton[1].setBackground(Color.WHITE);
                    }
                    if (finalI == 4) {
                        sugarButton[0].setBackground(Color.WHITE);
                        sugarButton[3].setBackground(Color.WHITE);
                        sugarButton[2].setBackground(Color.WHITE);
                        sugarButton[1].setBackground(Color.WHITE);
                    }
                    orderItem.specification = dispositionLabel.getText();
                }
            });
        }
        boolean[] b = new boolean[4];
        for (int i = 0; i < 4; i++) {
            int finalI = i;
            ingredientButton[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if ( !b[finalI]) {
                        ingredientButton[finalI].setBackground(Color.CYAN);
                        in = ingredientButton[finalI].getText();
                        dispositionLabel.setText(t + "/" + s + "/" + in);
                        if (finalI == 0) {
                            ingredientButton[2].setBackground(Color.WHITE);
                            ingredientButton[1].setBackground(Color.WHITE);
                            ingredientButton[3].setBackground(Color.WHITE);
                        }
                        if (finalI == 1) {
                            ingredientButton[0].setBackground(Color.WHITE);
                            ingredientButton[2].setBackground(Color.WHITE);
                            ingredientButton[3].setBackground(Color.WHITE);
                        }
                        if (finalI == 2) {
                            ingredientButton[0].setBackground(Color.WHITE);
                            ingredientButton[1].setBackground(Color.WHITE);
                            ingredientButton[3].setBackground(Color.WHITE);
                        }
                        if (finalI == 3) {
                            ingredientButton[0].setBackground(Color.WHITE);
                            ingredientButton[1].setBackground(Color.WHITE);
                            ingredientButton[2].setBackground(Color.WHITE);
                        }
                        orderItem.specification = dispositionLabel.getText();
                        b[finalI] = true;
                    }
                    else {
                        ingredientButton[finalI].setBackground(Color.WHITE);
                        b[finalI] = false;
                        dispositionLabel.setText(t + "/" + s);
                    }
                }
            });
        }
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            tempButton[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    tempButton[finalI].setBackground(Color.CYAN);
                    t = tempButton[finalI].getText();
                    dispositionLabel.setText(t + "/" + s + "/" + in);
                    if (finalI == 0) {
                        tempButton[2].setBackground(Color.WHITE);
                        tempButton[1].setBackground(Color.WHITE);
                    }
                    if (finalI == 1) {
                        tempButton[0].setBackground(Color.WHITE);
                        tempButton[2].setBackground(Color.WHITE);
                    }
                    if (finalI == 2) {
                        tempButton[0].setBackground(Color.WHITE);
                        tempButton[1].setBackground(Color.WHITE);
                    }
                    orderItem.specification = dispositionLabel.getText();
                }
            });
        }
        tempButton[0].setBackground(Color.CYAN);
        sugarButton[0].setBackground(Color.CYAN);
        t = tempButton[0].getText();
        s = sugarButton[0].getText();
        in = "";
        dispositionLabel.setText(t + "/" + s);
        temp.setBounds(20,170,100,30);
        temp.setFont(new Font("",Font.BOLD,20));
        sugar.setBounds(20,270,100,30);
        sugar.setFont(new Font("",Font.BOLD,20));
        ingredient.setBounds(20,320+j1*50+j*50,100,30);
        ingredient.setFont(new Font("",Font.BOLD,20));
    }
    public void setAboutMilk(MilkTea milkTea) {
        this.milkTea.milkName = milkTea.milkName;
        this.milkTea.imagePath = milkTea.imagePath;
        this.milkTea.price = milkTea.price;
        this.milkTea.inventory = milkTea.inventory;
        order = new Order();
        orderItem = new OrderItem();
        orderItem.milkName = milkTea.milkName;
        orderItem.price = milkTea.price;
        orderItem.imagePath = milkTea.imagePath;
        orderItem.specification = dispositionLabel.getText();
        orderItem.number = 1;
        Image image = new ImageIcon(milkTea.imagePath).getImage()
                .getScaledInstance(100,100,Image.SCALE_DEFAULT);
        milkLabel.setText(milkTea.milkName);
        milkLabel.setIcon(new ImageIcon(image));
        priceLabel.setText("" + milkTea.price);
    }
}
